import Main from "./main.vue";

const JuWaterMark = (options?: any) => {
  let timer: ReturnType<typeof setTimeout>;
  const parentElement = document.body;

  const loop = () => {
    // 生成随机插入点
    const randomIndex = Math.floor(Math.random() * parentElement.childNodes.length) - 1;
    parentElement.insertBefore( instance.$el, parentElement.childNodes[randomIndex]);

    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(loop, 1000);
  };

  const instance = new Main({
    data: options,
    beforeDestroy() {
      if (timer) {
        clearTimeout(timer);
      }
      parentElement.removeChild(this.$el);
    },
  });

  instance.$mount();

  loop();// 开始绘制

  return instance;
};

JuWaterMark.show = (options?: any) => JuWaterMark(options);

export default JuWaterMark;
